<template>
  <div>
    <div>【大区异常处理】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

// 获取 dom 实例
const target = ref(null)
let myChart = null
const getSeries = () => {
  const series = []
  const data = props.data.abnormals
  for (let i = 0; i < data.length; i++) {
    series.push({
      name: data[i].name,
      type: 'pie',
      label: { show: false },
      clockWise: false,
      hoverAnimation: false,
      data: [
        { value: data[i].value, name: data[i].name },
        {
          value: 1000,
          name: '',
          itemStyle: { color: 'rgba(0,0,0,0)', borderWidth: 0 },
          tooltip: { show: false },
          hoverAnimation: false
        }
      ],
      radius: [`${73 - 15 * i}%`, `${68 - 15 * i}%`]
    })
    series.push({
      name: data[i].name,
      type: 'pie',
      z: 1,
      clockWise: false,
      silent: true,
      hoverAnimation: false,
      label: { show: false },
      radius: [`${73 - 15 * i}%`, `${68 - 15 * i}%`],
      data: [
        // 绘制底线 75%
        {
          value: 7.5,
          itemStyle: { color: 'rgb(3, 31, 62)', borderWidth: 0 },
          tooltip: { show: false },
          hoverAnimation: false
        },
        // 绘制底线 25% 透明区域
        {
          value: 2.5,
          name: '',
          itemStyle: { color: 'rgba(0,0,0,0)', borderWidth: 0 },
          tooltip: { show: false },
          hoverAnimation: false
        }
      ]
    })
  }
  return series
}
const render = () => {
  const option = {
    legend: {
      show: true,
      icon: 'circle',
      top: '5%',
      left: '55%',
      width: -5,
      itemWidth: 10,
      // 每个色块的高度
      itemHeight: 10,
      // item 间距
      itemGap: 6,
      data: props.data.abnormals.map((item) => item.name),
      textStyle: {
        color: 'white',
        rich: {
          title: {
            fontSize: 12,
            lineHeight: 5,
            color: 'white'
          }
        }
      }
    },
    tooltip: {
      show: true,
      trigger: 'item',
      formatter: '{a}<br>{b}:{c}({d}%)'
    },
    series: getSeries()
  }
  myChart.setOption(option)
}
onMounted(() => {
  myChart = echarts.init(target.value)
  render()
})
watch(
  () => props.data,
  (newValue) => {
    render()
  }
)
</script>

<style lang="scss" scoped></style>
